import user from '../../api/user'
import * as types from '../mutation-types'
// initial state
const state = {
    user: null
}


// getters
const getters = {
    getUser: state => state.user
}

// actions
const actions = {
    userLogin({ commit }, userinfo) {
        console.log("user==>actions==>userLogin", commit, userinfo)
        user.login(userinfo, user => {
            commit(types.LOGIN_SUCCESS, { user })
            console.log("登录成功", user)

            router.push({ name: 'userinfo', params: { id: user.Name } })

        }, () => {
            commit(types.LOGIN_FAILURE)
        })
    }
}

const mutations = {
    [types.LOGIN_SUCCESS](state, action) {
        console.log("user==>mutations==>LOGIN_SUCCESS", action)
        state.user = action.user
    },
    [types.LOGIN_FAILURE](state, action) {
        console.log("modules==>user==>mutations==>LOGIN_FAILURE", action, state.user)
        state.user = null
    },
    [types.USERINFO_SUCCESS](state, action) {
        console.log("modules==>user==>mutations==>USERINFO_SUCCESS", action.user)
        state.user = action.user
    },
    [types.USERINFO_FAILURE](state, action) {
        console.log("modules==>user==>mutations==>USERINFO_FAILURE", action, state.user)
        state.user = null
    },
    [types.LOGOUT_USER](state, action) {
        state.token = getCookie('token') || null
        state.user = null
        state.token = null
    },
    [types.UPDATE_USER_SUCCESS](state, action) {
        state.user = action.user
    }
}


export default {
    state,
    getters,
    actions,
    mutations
}